package com.pride1952.myoptimusx3;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ContactActivity extends Activity implements AdapterView.OnItemClickListener{
    /**获取库Phon表字段**/
    private static final String[] PHONES_PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Photo.PHOTO_ID,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID };

    /**联系人显示名称**/
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;

    /**电话号码**/
    private static final int PHONES_NUMBER_INDEX = 1;

    /**头像ID**/
    private static final int PHONES_PHOTO_ID_INDEX = 2;

    /**联系人的ID**/
    private static final int PHONES_CONTACT_ID_INDEX = 3;

    String phoneNumber = null;

    String contactName = null;

    Long contactid = null;

    Long photoid = null;

    Bitmap contactPhoto = null;

    Cursor cursor=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        Intent intent=getIntent();
        int position=intent.getIntExtra("Position", 0);
        getPhoneContacts(position);
        showOnActivity();
    }
    private void getPhoneContacts(int position) {
        ContentResolver resolver = getContentResolver();
        cursor = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                null,
                null,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
        cursor.moveToPosition(position);

        if(cursor!=null)
        {
            //得到手机号码
            phoneNumber = cursor.getString(cursor.getColumnIndex(PHONES_PROJECTION[PHONES_NUMBER_INDEX]));
            //当手机号码为空的或者为空字段 跳过当前循环
            if (TextUtils.isEmpty(phoneNumber))
                return;

            //得到联系人名称
            contactName = cursor.getString(cursor.getColumnIndex(PHONES_PROJECTION[PHONES_DISPLAY_NAME_INDEX]));

            //得到联系人ID
            contactid = cursor.getLong(cursor.getColumnIndex(PHONES_PROJECTION[PHONES_CONTACT_ID_INDEX]));

            //得到联系人头像ID
            photoid = cursor.getLong(cursor.getColumnIndex(PHONES_PROJECTION[PHONES_PHOTO_ID_INDEX]));

            //得到联系人头像Bitamp
            contactPhoto = null;

            //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
            if(photoid > 0 ) {
                Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
                InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
                contactPhoto = BitmapFactory.decodeStream(input);
            }else {
                contactPhoto = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            }
            cursor.close();
        }
    }
    void showOnActivity()
    {
        //将数据库中的数据显示到界面上
        TextView txtName=(TextView)findViewById(R.id.txt_contact_name);
        txtName.setText(contactName);
        TextView txtStuNum=(TextView)findViewById(R.id.txt_contact_stu_num);

        TextView txtNumber=(TextView)findViewById(R.id.txt_contact_number);
        txtNumber.setText(phoneNumber);
        TextView txtMail=(TextView)findViewById(R.id.txt_contact_mail);

        TextView txtHobby=(TextView)findViewById(R.id.txt_contact_hobby);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_contact, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
